package com.dqyt.app.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.ReportLhq;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.mongo.ReportLhqDao;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.HistoryBean;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.config.Config;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import com.dqyt.quart.AddSimpleJobReq;
import com.dqyt.quart.DeleteJobReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AppLhqReportService {

    @Autowired
    private ReportLhqDao reportLhqDao;



    /**
     *  分页查询报告
     * @param request
     * @param userId  用户id
     * @param token
     * @param addTime
     * @param reportStatue  报告状态
     * @param bsbh  标识编号
     * @param erweima  二维码
     * @param deviceType  设备类型
     * @param reportProgress  报告进度
     * @return
     */
    public ResultMsg getReportInfosLhq(HttpServletRequest request, Integer page, Integer size, long userId, String token, long addTime, int reportStatue, String bsbh, String erweima, int deviceType, int reportProgress, long wtfId) {

        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 简单的数据检查
            if (addTime == 0) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            ret = new ResultMsg();
            // 取得条件数据
            List<ReportLhq> Reports = reportLhqDao.getReportInfosLhq(page,size,addTime, reportStatue, bsbh, erweima,deviceType,reportProgress,wtfId);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(JSON.toJSONString(Reports));
            return ret;
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }





    /**
     * 驳回报告
     * @param request
     * @param userId
     * @param token
     * @param bh
     * @param reportProgress
     * @param bhgyy
     * @return
     */
    public ResultMsg  rejectLhqReportDal(HttpServletRequest request, long userId, String token, String bh, int reportProgress, String bhgyy){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            User user = UserInfoUtil.getUserInfologin(userId, token);
            if(null == user){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("登录过期");
                return  ret;
            }

            //先查询老的报告
            ReportLhq report = reportLhqDao.getReportByBH(bh);
            report.reportProgress = reportProgress;
            //任何情况被驳回都要清除  审核员和 批准员

            //设置核验员的id为自己的id
            report.pzrId = "";
            report.pzr = "";
            report.hyyId = "";
            report.hyy = "";

            //不合格原因
            report.bhyy = bhgyy;
            long time = TimeUtil.getCurrentTimeMillis();
            reportLhqDao.save(report);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("成功");
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 报告通过审核
     *
     * @param request
     * @param bh
     * @return
     */
    public ResultMsg reportLhqAdoptExamine(HttpServletRequest request,long userId, String token, String bh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            User user = UserInfoUtil.getUserInfologin(userId, token);
            if (null == user) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("登录过期");
                return ret;
            }

            if(0 == user.getSealId()){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你还没有电子签章");
                return ret;
            }
            //先查询老的报告
            ReportLhq report = reportLhqDao.getReportByBH(bh);
            //设报告的进度为待审批
            report.reportProgress = 3;

            //设置核验员的id为自己的id
            report.hyyId = String.valueOf(user.getUserId());
            report.hyy = user.getUserName();

            long time = TimeUtil.getCurrentTimeMillis();
            reportLhqDao.save(report);
            ret.setCode(ResultMsg.SUCESS);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 报告通过 批准
     *
     * @param request
     * @param bh
     * @return
     */
    public ResultMsg reportLhqAdoptApproval(HttpServletRequest request,long userId, String token, String bh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            User user = UserInfoUtil.getUserInfologin(userId, token);
            if (null == user) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("登录过期");
                return ret;
            }

            if(0 == user.getSealId()){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你还没有电子签章");
                return ret;
            }
            //先查询老的报告
            ReportLhq report = reportLhqDao.getReportByBH(bh);
            //设报告的进度为 已完成
            report.reportProgress = 4;
            //设置核验员的id为自己的id
            report.pzrId = String.valueOf(user.getUserId());
            report.pzr = user.getUserName();
            long time = TimeUtil.getCurrentTimeMillis();
            reportLhqDao.save(report);
            ret.setCode(ResultMsg.SUCESS);

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


}
